
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {


    public static void main(String[] args) {
        String[] data = new String[]{
                "abcd", "adc", "abcda", "abceda", "abDc", "abdc", "acd", "xyza", "xybcd", "xyzab",
                "1234", "12a34", "1a2b3c4d", "aabbccdd", "a1b2c3d4", "9876", "9a87b6c5d", "a1b2c3", "abcdefg", "aabb"
        };        Scanner scanner = new Scanner(System.in);

        // 使用一个标志变量来控制循环
        boolean continueInput = true;

        while (continueInput) {
            // 提示用户输入内容
            System.out.print("请输入内容：");
            // 读取用户输入的内容
            String userInput = scanner.nextLine();
            String[] result=filterAndSort(data,userInput);
            // 打印用户输入的内容
            for (String s : result) {
                System.out.println(s);
            }
        }

        // 关闭Scanner
        scanner.close();
    }

/*    public static List<Map<String,String>> sort(List<Map<String,String>> data,String input){
        Map<String, Map<String, String>> dataMap = new HashMap<>();
        List<String> cmdValueList = new ArrayList<>();
        for (Map<String, String> datum : data) {
            String cmdValue=datum.get("cmd");
            cmdValueList.add(cmdValue);
            dataMap.put(cmdValue, datum);
        }
        String[] filterAndSortArr=filterAndSort(data.toArray(new String[0]), input);
        List<Map<String, String>> ret = new ArrayList<>();
        for (String s : filterAndSortArr) {
            ret.add(dataMap.get(s));
        }
        return ret;
    }*/

    public static String[] filterAndSort(String[] data,String input){
        //过滤不符合要求的字符
        data=getMatchingStrings(input, data);
        data = sortData(data, input);
        //排序
        return data;
    }

    public static String[] sortData(String[] data,String input){
        Map<String, Integer> scoreMap = new HashMap<>();
        for (int k = 0; k < data.length; k++) {
            int score = Integer.MAX_VALUE;
            String current=data[k];
            for (int i = 0; i < input.length(); i++) {
                int index=current.indexOf(input.charAt(i));
                char currentChar=input.charAt(i);
                if (index < 0) {
                    index = current.indexOf(Character.toUpperCase(currentChar));
                    if (index < 0) {
                        score=score-(input.length()+i);
                    }else{
                        score=score-(index+1);
                    }
                }else{
                    score=score-index;
                }
                if (index > 0) {
                    current = current.substring(0, index) + current.substring(index + 1);
                }
            }
            scoreMap.put(data[k], score);
        }
        String[] sortedKeys = scoreMap.entrySet()
                .stream()
                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()
                        .thenComparing(Map.Entry.comparingByKey(Comparator.comparingInt(String::length))))
                .map(Map.Entry::getKey)
                .toArray(String[]::new);
        return sortedKeys;
    }

    public static String addAsteriskAfterEachChar(String input) {
        StringBuilder result = new StringBuilder(".*");
        for (int i = 0; i < input.length(); i++) {
            result.append(input.charAt(i)).append(".*");
        }
        return result.toString();
    }

    public static String[] getMatchingStrings(String pattern, String[] inputs) {
        String regex = "(?i)" + addAsteriskAfterEachChar(pattern);
        Pattern compiledPattern = Pattern.compile(regex);

        List<String> matchingStrings = new ArrayList<>();
        for (String input : inputs) {
            Matcher matcher = compiledPattern.matcher(input);
            if (matcher.matches()) {
                matchingStrings.add(input);
            }
        }

        return matchingStrings.toArray(new String[0]);
    }


}
